﻿@page "/Administrator"

@using Kite.Gateway.Application.Contracts
@using Kite.Gateway.Application.Contracts.Dtos.Administrator
@using Mapster
@using Kite.Gateway.Domain.Shared.Enums
@inject ToastService ToastService
@inject IAdministratorAppService AdministratorAppService

<div class="row">
    <div class="col=12">
        <Button OnClick="@OnAddAsync" Color="Color.Primary">添加管理员</Button>
    </div>
</div>
<div class="row" style="margin-top:10px;">
    <table class="table">
      <thead class="table-light">
        <tr>
            <th>管理员名</th>
            <th>昵称</th>
            <th>创建时间</th>
            <th>更新时间</th>
            <th>操作</th>
        </tr>
      </thead>
      <tbody>
          @if(Administrators!=null&&Administrators.Any())
          {
              foreach(var item in Administrators)
              {
                <tr>
                    <td>@item.AdminName</td>
                    <td>@item.NickName</td>
                    <td>@item.Created</td>
                    <td>@item.Updated</td>
                    <td>
                        <div class="btn-group" role="group">
                            <Button Color="Color.Info" Size="Size.ExtraSmall" OnClick="@(e=>OnUpdateAsync(item.Id))">编辑</Button>
                            <PopConfirmButton  Size="Size.ExtraSmall" Placement="Placement.Bottom" Color="Color.Danger" ConfirmIcon="fa fa-exclamation-triangle text-danger"
                  ConfirmButtonColor="Color.Danger" Text="删除" Content="确定删除数据吗？" Icon="fa fa-fa fa-fw" IsAsync="true"
                  OnConfirm="@(()=>OnDeleteAsync(item.Id))" />
                        </div>
                    </td>
                </tr>
              }
            }
        </tbody>
    </table>
    
</div>
<div class="row col-12"  style="margin-top:15px;">
    <Pagination PageItems="@PageSize" PageItemsSource="@(new int[] { 10,20 })" TotalCount="@TotalCount" OnPageClick="@OnPageClick" OnPageItemsChanged="@OnPageItemsChanged"></Pagination>
</div>

<Modal @ref="Modal">
    <ModalDialog  IsScrolling="true" Title="添加管理员" IsCentered="true" Size="Size.Large" ShowCloseButton="false">
        <BodyTemplate >
          <NewAdministrator OnClickCallback="OnAddCallbackAsync"></NewAdministrator>
        </BodyTemplate>
    </ModalDialog>
</Modal>

<Modal @ref="UpdateModal">
    <ModalDialog  IsScrolling="true" Title="管理员编辑" IsCentered="true" Size="Size.Large" ShowCloseButton="false">
        <BodyTemplate >
           <UpdateAdministrator Model="@UpdateModel" OnClickCallback="OnUpdateCallbackAsync"></UpdateAdministrator>
        </BodyTemplate>
    </ModalDialog>
</Modal>

@code {
    private Modal Modal;

    private Modal UpdateModal;

    public UpdateAdministratorDto UpdateModel { get; set; }
    //分页数据

    private List<AdministratorDto>? Administrators { get; set; }

    private int TotalCount { get; set; } = 0;
    private int PageSize { get; set; } = 10;
    private int PageIndex { get; set; } = 1;
    /// <summary>
    /// OnInitialized 方法
    /// </summary>
    protected override void OnInitialized()
    {
        OnQueryAsync();
        base.OnInitialized();
    }
    #region 管理员编辑更新
    private async void OnUpdateAsync(Guid id)
    {
        UpdateModel= await GetAsync(id);
        await UpdateModal.Toggle();
    }
    private async Task<UpdateAdministratorDto> GetAsync(Guid id)
    {
        var result = await AdministratorAppService.GetAsync(id);
        if (result.Code != 0 || result.Data == null)
        {
            await ToastService.Warning("系统警告", "数据不存在");
        }
        var model= TypeAdapter.Adapt<UpdateAdministratorDto>(result.Data);
        return model;
    }
    private async void OnUpdateCallbackAsync()
    {
        await UpdateModal.Close();
        OnQueryAsync();
    }
    #endregion
    #region 新增管理员
    private async void OnAddAsync()
    {
        await Modal.Toggle();
    }
    private async void OnAddCallbackAsync()
    {
        await Modal.Close();
        OnQueryAsync();
    }
    #endregion
    private async Task OnDeleteAsync(Guid id)
    {
        var result= await AdministratorAppService.DeleteAsync(id);
        if (result.Code == 0)
        {
            OnQueryAsync();
            await ToastService.Success("系统通知","数据删除成功!");
            return;
        }
        await ToastService.Warning("系统警告","数据删除失败");
    }

    private Task OnPageClick(int pageIndex, int pageItems)
    {
        PageIndex = pageIndex;
        OnQueryAsync();
        return Task.CompletedTask;
    }
    private Task OnPageItemsChanged(int pageItems)
    {
        PageIndex=1;
        PageSize = pageItems;
        OnQueryAsync();
        return Task.CompletedTask;
    }

    private async void OnQueryAsync()
    {
        var result = await AdministratorAppService.GetListAsync(PageIndex, PageSize);

        if (result.Code == 0)
        {
            Administrators = result.Data;
            TotalCount = result.Count;
            StateHasChanged();
        }
        else
        {
            await ToastService.Warning("系统警告", "数据加载失败");
        }
    }

}
